<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2018/11/23
 * Time: 下午8:45
 */

namespace Api\Service;


use Mobile\Controller\WechatController;

class ThirdService extends BaseService
{

    public $wechat;
    public function __construct()
    {
        $this->wechat=new WechatController();
    }

    /*
     * 生成推广二维码图片
     */
    public function makeshare($userinfo,$refresh=0){

        $uid = $userinfo['uid'];
        $nickname = $userinfo['realname'] ? $userinfo['realname'] : $userinfo['nickname'];
        if(!$nickname){
            $nickname = C("SITE_NAME");
        }
        $file = ROOT.'/upload/share/qr'.$uid.".jpg";//存储的文件名
        $pic_url = get_domain()."/upload/share/".$uid.".jpg";
        if(file_exists($file) && !$refresh){
            //如果文件存在 并且不需要更新
            return $pic_url;
        }
        //获取二维码图片
        $qrcode_pic = $this->qrcodeurl($uid,$refresh);
        if(!$qrcode_pic){
            return false;//生成二维码失败
        }
        $grade = $userinfo['grade_id'] ? $userinfo['grade_id'] : 1;
        $background_url = C("QRCODE_GRADE".$grade);
        $arr = array(
            array(
                'text' => qiniubase64url($userinfo['nickname']),
                'dx' => 0,
                'dy' => 180,
                'gravity' => 'North',
                'fontsize' => '600',
                'fill' => qiniubase64url('#ffffff'),
            ),
            array(
                'text' => qiniubase64url('ID: '.$uid),
                'dx' => 0,
                'dy' => 240,
                'gravity' => 'North',
                'fontsize' => '400',
                'fill' => qiniubase64url('#ffffff'),
            ),
            array(
                'image' => qiniubase64url($qrcode_pic),
                'dx' => 0,
                'dy' => -5,
                'gravity' => 'Center',
                'ws' => 0.43
            )
        );
        if(!empty($arr)) {
            $background_url .= "?watermark/3";
        }
        foreach($arr as $v) {
            if($v['text']) {
                $background_url .= "/text/{$v['text']}";
                if($v['fontsize']) {
                    $background_url .= "/fontsize/{$v['fontsize']}";
                }
                if($v['fill']) {
                    $background_url .= "/fill/{$v['fill']}";
                }
            }else{
                $background_url .= "/image/{$v['image']}";
            }
            $background_url .= "/dx/{$v['dx']}";
            $background_url .= "/dy/{$v['dy']}";
            $background_url .= "/gravity/{$v['gravity']}";
            if($v['ws']) {
                $background_url .= "/ws/{$v['ws']}";
            }

        }
        return $background_url;
    }


    //获取二维码地址
    public function qrcodeurl($uid,$refresh=0){

        /* $text = get_domain()."/mobile/login/register?refer=".$uid;//内容
        $pic_url = "http://qr.liantu.com/api.php?text=".$text;//图片网址
        return $pic_url; */

        $pic_url = get_domain()."/upload/qrcode/qr".$uid.".png";//图片网址
        $outfile = ROOT."/upload/qrcode/qr".$uid.".png";
        if(!file_exists($outfile) || $refresh){

            //qrcode($text, $outfile, $level = 1, $size = 8, $margin = 1,$saveandprint=false);

            $scene_id = "FENXIAO_UID_".$uid;
            $ticket_arr = $this->wechat->getQRCode($scene_id,2);//获取二维码ticket
            if(isset($ticket_arr['url'])){
                //getQRUrl($ticket)
                $text = $ticket_arr['url'];
                //return $pic_url = "http://qr.liantu.com/api.php?text=".$text;
                qrcode($text, $outfile, $level = 1, $size = 8, $margin = 1,$saveandprint=false);
            }

            if(!file_exists($outfile)){
                //不存在,生成失败
                return false;
            }
        }
        return $pic_url;
    }


}